package wiView.converter.tracetoxml;
import java.awt.Point;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;

public class CbrPacketToXML {
	public static FileOutputStream fout;
	public static OutputStreamWriter out;
	public static String sCurrentLine;
	public static ArrayList<Packet> listPacket;
	public static ArrayList<Node> listNodes;
	
		public static void main(String[] args) throws IOException{
		    fout = new FileOutputStream("TextToXML_1.xml");
			out = new OutputStreamWriter(fout);
			out.write("<?xml version=\"1.0\"?>\r\n");  
		    out.write("<trace>\r\n\r\n");  
		   
		    listPacket=new ArrayList<Packet>();
		    listNodes=new ArrayList<Node>();
		    
		    Init(); 
		    CreateNodeElements();
		    CreatePacketElements();
			CreateEventElements();
			
			out.write("</trace>");
			out.close();
			
			System.out.println("Finish");
		 
			
		}		
			
		public static void Init() throws IOException{
			BufferedReader br = new BufferedReader(new FileReader("trace(2).tr"));
			String retval[];
			Node node=null;
			Packet packet=null;
			while ((sCurrentLine = br.readLine()) != null){ 
			    retval = sCurrentLine.split(" ");
			    //init packet
			    if(!retval[0].equals("N") && !retval[0].equals("M") ){
			    	//packe cbr
			    	if(retval[7].equals("cbr") ){
			    		
			    		if(retval[0].equals("s") && retval[3].equals("RTR") ){
			    		packet=new Packet(Integer.parseInt(retval[6]));
			    		packet.sourceID=Integer.parseInt(retval[2].substring(1, retval[2].length()-1));
			    		packet.type=retval[7];
			    		packet.size=Integer.parseInt(retval[8]);
			    		packet.sourcePort=0;
			    		packet.destPort=0;
			    		
			    		packet.listNodeDest=new ArrayList<Node>();
			    		node=new Node(packet.sourceID,retval[1]);
			    		packet.listNode=new ArrayList<Node>();
			    		packet.listNode.add(node);
			    		listPacket.add(packet);
						
			    		}
			    	
			    		if(retval[0].equals("r")){
			    			if(retval[3].equals("AGT")){
			    				for(int i=0;i<listPacket.size();i++){
			    				packet=listPacket.get(i);
			    				if(packet.id==Integer.parseInt(retval[6]))
			    					break;
			    				}
			    				packet.destID=Integer.parseInt(retval[2].substring(1, retval[2].length()-1));
			    				packet.listNode.add(new Node(Integer.parseInt(retval[2].substring(1, retval[2].length()-1)),retval[1]) );		    				
			    			}
			    		}
					
			    		if(retval[0].equals("f")){
				    			for(int i=0;i<listPacket.size();i++){
				    				packet=listPacket.get(i);
				    				if(packet.id==Integer.parseInt(retval[6]))
				    					break;
				    				}
				    			packet.listNode.add(new Node(Integer.parseInt(retval[2].substring(1, retval[2].length()-1)),retval[1]) );
			    		}
			    	}
			    	//packet boundhole
			    	if(retval[7].equals("BOUNDHOLE") || retval[6].equals("BOUNDHOLE") ){
			    		if(retval[0].equals("s")) {
				    		packet=new Packet(Integer.parseInt(retval[6]));
				    		packet.sourceID=Integer.parseInt(retval[2].substring(1, retval[2].length()-1));
				    		packet.type=retval[7];
				    		packet.size=Integer.parseInt(retval[8]);
				    		
				    		packet.listNodeDest=new ArrayList<Node>();
				    		node=new Node(packet.sourceID,retval[1]);
				    		packet.listNode=new ArrayList<Node>();
				    		packet.listNode.add(node);
				    		listPacket.add(packet);	
				    	}
			    		if(retval[0].equals("D")){
			    			for(int i=0;i<listPacket.size();i++){
			    				packet=listPacket.get(i);
			    				if(packet.id==Integer.parseInt(retval[5]))
			    					break;
			    				}
			    			if(retval[4].equals("HELLO")){
			    				packet.destID=-1;
			    				packet.sourcePort=255;
					    		packet.destPort=-1;
			    				packet.listNodeDest.add(new Node(Integer.parseInt(retval[2].substring(1, retval[2].length()-1)),retval[1]) );
			    			}
			    			else{ 
			    				packet.listNode.add(new Node(Integer.parseInt(retval[2].substring(1, retval[2].length()-1)),retval[1]) );
			    				packet.destID=Integer.parseInt(retval[2].substring(1, retval[2].length()-1));
			    				packet.sourcePort=0;
					    		packet.destPort=0;
			    			}
			    		}
			    		if(retval[0].equals("f")){
			    			for(int i=0;i<listPacket.size();i++){
			    				packet=listPacket.get(i);
			    				if(packet.id==Integer.parseInt(retval[6]))
			    					break;
			    				}
			    			packet.listNode.add(new Node(Integer.parseInt(retval[2].substring(1, retval[2].length()-1)),retval[1]) );
			    		}
			    		
			    	}
			    	//packet goal
			    	if(retval[7].equals("GOAL") ){
			    		
			    	}
			    	
			    }
			    //init Node
			    if(retval[0].equals("N") ){
			    	node=new Node(Integer.parseInt(retval[4]),retval[2]);
			    	node.energy=retval[6];
			    	listNodes.add(node);
			    }
			 }
			br.close();
		}
		
		public static void CreateNodeElements() throws IOException {
			 Node node;
			 out.write("  <node>\r\n"); 
			 for (int i=0;i<listNodes.size();i++){ 
				 node=listNodes.get(i);
				 out.write("    <node id='"+node.id+"' time='"+node.time+"' x='' y='' energy='"+node.energy+"'></node>"+"\r\n");
			 }
			 
			 out.write("  </node>\r\n\r\n"); 	
		}
		
		public static void CreatePacketElements() throws IOException {
			Packet packet;
			out.write("  <packet>\r\n"); 
			 for (int i=0;i<listPacket.size();i++){ 
				 packet=listPacket.get(i);
				 out.write("    <packet id='"+packet.id+"' type='"+packet.type+"' " +
					"source-id='"+packet.sourceID+"' source-port='"+packet.sourcePort+"' dest-id='"+packet.destID+"' " +
					"dest-port='"+packet.destPort+"' size='"+packet.size+"'></packet>"+"\r\n");		
			 }
			 out.write("  </packet>\r\n\r\n"); 
		}

		private static void CreateEventElements() throws IOException {
			Packet packet;
			int j;
			out.write("  <event>\r\n"); 
			for (int i=0;i<listPacket.size();i++){ 
				packet=listPacket.get(i);
				if(packet.listNode.size()>=2 && packet.listNodeDest.size()==0){
				out.write("    <event type='send' time='"+packet.listNode.get(0).time+"' packet-id='"+packet.id+"' " +
						       "source-id='"+packet.sourceID+"' source-port='"+packet.sourcePort+"' dest-id='"+packet.listNode.get(1).id+"' " +
							   "dest-port='"+packet.destPort+"' remain-energy=''></event>"+"\r\n");	
			    for(j=1;j<packet.listNode.size()-1;j++){
			    	out.write("    <event type='forward' time='"+packet.listNode.get(j).time+"' packet-id='"+packet.id+"' " +
						       "source-id='"+packet.listNode.get(j).id+"' source-port='"+packet.sourcePort+"' dest-id='"+packet.listNode.get(j+1).id+"' " +
							   "dest-port='"+packet.destPort+"' remain-energy=''></event>"+"\r\n");
			    }
			    out.write("    <event type='receive' time='"+packet.listNode.get(j).time+"' packet-id='"+packet.id+"' " +
					       "source-id='"+packet.sourceID+"' source-port='"+packet.sourcePort+"' dest-id='"+packet.destID+"' " +
						   "dest-port='"+packet.destPort+"' remain-energy=''></event>"+"\r\n");	
				}
				if(packet.listNode.size()<2 && packet.listNodeDest.size()==0){
					out.write("    <event type='send' time='"+packet.listNode.get(0).time+"' packet-id='"+packet.id+"' " +
						       "source-id='"+packet.sourceID+"' source-port='"+packet.sourcePort+"' dest-id='-1' " +
							   "dest-port='"+packet.destPort+"' remain-energy=''></event>"+"\r\n");	
				} 
				if(packet.listNodeDest.size()>=1){
					out.write("    <event type='send' time='"+packet.listNode.get(0).time+"' packet-id='"+packet.id+"' " +
						       "source-id='"+packet.sourceID+"' source-port='"+packet.sourcePort+"' dest-id='"+packet.destID+"' " +
							   "dest-port='"+packet.destPort+"' remain-energy=''></event>"+"\r\n");
					 for(int k=0;k<packet.listNodeDest.size();k++){
					    	out.write("    <event type='receive' time='"+packet.listNodeDest.get(k).time+"' packet-id='"+packet.id+"' " +
								       "source-id='"+packet.sourceID+"' source-port='"+packet.sourcePort+"' dest-id='"+packet.listNodeDest.get(k).id+"' " +
									   "dest-port='"+packet.destPort+"' remain-energy=''></event>"+"\r\n");
					    }
				}
			}
			out.write("  </event>\r\n\r\n");
		}
		

}
